package com.pigcloud.sop.gateway.config;


import lombok.Data;
import lombok.Getter;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;


/**
 * @author LiuYang
 * @date 2023/12/14 15:56
 **/
@Data
@Configuration
public class JwtTokenConfig {

    @Getter
    private final String privateKey = "4900469196959520693080527714660984660633446104815764985075156967423043486776729380849921268644654797331242447749316418911390430941408306197322285985422518852618576359794544670676730030335256582251080633207607514586364958265577358541757901411911418013198135122369543349089292720917377979944052707081189080043930873650459117464879497664659070774095443107861551130392305155600206049890455589664147416903950592565499421992217430635846086513298374945335329455018401686261332656122380267285722473154858185344497566434044829262142748392120053821273431792294746881932791711080082160342983007389936290415783373579346464630817";
    @Getter
    private final String publicKey = "22158275821797716825059924440163995563952956904326672404227305241755485096790845414154911700232147195577317255288184776196756911315515120964116921252010184769150244162540021669804115909899386686283225573239062624772085019308068397043686186341412902051260258142316321406738303922365268433254683473435896835852313223695353504350045198137327223374217168677944407861609540669147389427707023872606783076074769651693564314868544323348915755284294725240920920088858225841924820299048558600402017878165257167012536626421804390268913716636423407061540822051364312987569477067086238407229038480646774132895764674431694859785841";

    private final static String KEY_ID = "9996a037-9fb4-43e2-a13d-00fc902a0236";


    public RSAPublicKey createRsaPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger publicKeyBigNumber = new BigInteger(publicKey);
        BigInteger publicKeyPublicExponent = new BigInteger("65537");
        BigInteger privateKeyBigNumber = new BigInteger(privateKey);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");


        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(publicKeyBigNumber, publicKeyPublicExponent);
        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(publicKeyBigNumber, privateKeyBigNumber);

        return (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    }

    public RSAPrivateKey createRsaPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger publicKeyBigNumber = new BigInteger(publicKey);
        BigInteger publicKeyPublicExponent = new BigInteger("65537");
        BigInteger privateKeyBigNumber = new BigInteger(privateKey);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(publicKeyBigNumber, privateKeyBigNumber);

        return (RSAPrivateKey) keyFactory.generatePrivate(priKeySpec);
    }
    @SneakyThrows
    @Bean
    public JwtDecoder jwtDecoder() {
        // Replace with your own public key
        RSAPublicKey rsaPublicKey = createRsaPublicKey();
        return NimbusJwtDecoder.withPublicKey(rsaPublicKey).build();
    }

}
